package LeetCodeHot100;

/**
 * 判断环形链表
 * 思路：使用快慢指针，快指针先跑，慢指针后跑，
 *      如果有环，快指针总会跟慢指针相遇
 */

public class LeetCode141 {

    public boolean hasCycle(ListNode head) {
        if(head == null || head.next == null){
            return false;
        }
        ListNode slow = head;
        ListNode fast = head.next;

        while (slow!=fast){
            if(fast == null || fast.next == null){
                return false;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        return true;
    }

    class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
      }
  }
}
